package com.candy.control;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
       String initparam= config.getInitParameter("initparam");
       System.out.println("获取在配置中的initparam的值：：："+initparam);
//       获取servletContext对象
        ServletContext context=  config.getServletContext();
       String contextParam =context.getInitParameter("contextparam");
        System.out.println("获取在配置中的contextParam的值：：："+contextParam);


    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        当请求为get请求时，则进行调用post方法进行处理
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收前端传递过来的请求
        System.out.println("已接收到客户端发来的请求.....");
        //       首先处理字符集，避免中文乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        获取请求携带的参数
       String username= req.getParameter("username");
        String userpwd= req.getParameter("userpwd");
        System.out.println("前端传递过来的用户名：：：："+username);
        System.out.println("前端传递过来的密码：：：："+userpwd);
//        将数据存放在请求域中
        req.setAttribute("username",username);
//        将数据存放在session
        req.getSession().setAttribute("role","vip");
//        转发，实现页面跳转
//        req.getRequestDispatcher("welcome.jsp").forward(req,resp);
//        重定向
        resp.sendRedirect("welcome.jsp");



    }
}
